package com.miblog.controller.admin;

import com.miblog.domain.User;
import com.miblog.repository.UserRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by liuzhengyang on 2015/4/7.
 */
@Controller
@RequestMapping("/admin/user")
public class UserControllerAdmin {

    private final Logger logger = LoggerFactory.getLogger(UserControllerAdmin.class);

    @Autowired
    private UserRepository userRepository;

    @RequestMapping("/users")
    public String users(Model model){
        model.addAttribute("users", userRepository.findAll());
        logger.info(userRepository.findAll().toString());
        return "/admin/users";
    }
    @RequestMapping("/view/{id}")
    public String view(Model model, @PathVariable("id")Long id){
        model.addAttribute("user", userRepository.findOne(id));
        logger.info(model.asMap().toString());
        return "/admin/user";
    }
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public String save(User user){
        logger.info(user.getId()+"");
        userRepository.save(user);
        logger.info(user.toString());
        return "redirect:/admin/user/users";
    }


}
